home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / Level 1 Extensions 29Sep94 / CheckBox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  7.8 KB  |  296 lines  |  [TEXT/KAHL]

  1. /* CheckBox.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    System Dependency Library for Building Portable Software               */
  5. /*    Macintosh Version                                                      */
  6. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  7. /*                                                                           */
  8. /*    This file is Public Domain; it may be used for any purpose whatsoever  */
  9. /*    without restriction.                                                   */
  10. /*                                                                           */
  11. /*    This package is distributed in the hope that it will be useful,        */
  12. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  13. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  14. /*                                                                           */
  15. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18.  
  19. #include "MiscInfo.h"
  20. #include "Audit.h"
  21. #include "Debug.h"
  22. #include "Definitions.h"
  23.  
  24. #include "CheckBox.h"
  25. #include "Screen.h"
  26. #include "Memory.h"
  27. #include "EventLoop.h"
  28. #include "DataMunging.h"
  29.  
  30.  
  31. struct CheckBoxRec
  32.     {
  33.         WinType*        Window;
  34.         char*                Name;
  35.         OrdType            X;
  36.         OrdType            Y;
  37.         OrdType            Width;
  38.         OrdType            Height;
  39.         MyBoolean        State;
  40.     };
  41.  
  42.  
  43. static unsigned char                oNormalUnselected[] =
  44.     {
  45.         0xFF,0xF0,0x80,0x10,0x80,0x10,0x80,0x10,0x80,0x10,0x80,0x10,
  46.         0x80,0x10,0x80,0x10,0x80,0x10,0x80,0x10,0x80,0x10,0xFF,0xF0
  47.     };
  48.  
  49. static unsigned char                oNormalSelected[] =
  50.     {
  51.         0xFF,0xF0,0x80,0x10,0x80,0x10,0x9F,0x90,0x9F,0x90,0x9F,0x90,
  52.         0x9F,0x90,0x9F,0x90,0x9F,0x90,0x80,0x10,0x80,0x10,0xFF,0xF0
  53.     };
  54.  
  55. static unsigned char                oMouseDownUnselected[] =
  56.     {
  57.         0xFF,0xF0,0xFF,0xF0,0xC0,0x30,0xC0,0x30,0xC0,0x30,0xC0,0x30,
  58.         0xC0,0x30,0xC0,0x30,0xC0,0x30,0xC0,0x30,0xFF,0xF0,0xFF,0xF0
  59.     };
  60.  
  61. static unsigned char                oMouseDownSelected[] =
  62.     {
  63.         0xFF,0xF0,0xFF,0xF0,0xC0,0x30,0xDF,0xB0,0xDF,0xB0,0xDF,0xB0,
  64.         0xDF,0xB0,0xDF,0xB0,0xDF,0xB0,0xC0,0x30,0xFF,0xF0,0xFF,0xF0
  65.     };
  66.  
  67. static long                        BitmapReferenceCount = 0;
  68.  
  69. static Bitmap*                NormalUnselected;
  70. static Bitmap*                NormalSelected;
  71. static Bitmap*                MouseDownUnselected;
  72. static Bitmap*                MouseDownSelected;
  73.  
  74.  
  75. /* create a new check box.  Name is null-terminated.  returns NIL if it failed */
  76. CheckBoxRec*        NewCheckBox(WinType* Window, char* Name,
  77.                                     OrdType X, OrdType Y, OrdType Width, OrdType Height)
  78.     {
  79.         CheckBoxRec*    TheButton;
  80.  
  81.         if (BitmapReferenceCount == 0)
  82.             {
  83.                 NormalUnselected = MakeBitmap(oNormalUnselected,12,12,2);
  84.                 if (NormalUnselected == NIL)
  85.                     {
  86.                      FailurePoint1:
  87.                         return NIL;
  88.                     }
  89.                 NormalSelected = MakeBitmap(oNormalSelected,12,12,2);
  90.                 if (NormalSelected == NIL)
  91.                     {
  92.                      FailurePoint2:
  93.                         DisposeBitmap(NormalUnselected);
  94.                         goto FailurePoint1;
  95.                     }
  96.                 MouseDownUnselected = MakeBitmap(oMouseDownUnselected,12,12,2);
  97.                 if (MouseDownUnselected == NIL)
  98.                     {
  99.                      FailurePoint3:
  100.                         DisposeBitmap(NormalSelected);
  101.                         goto FailurePoint2;
  102.                     }
  103.                 MouseDownSelected = MakeBitmap(oMouseDownSelected,12,12,2);
  104.                 if (MouseDownSelected == NIL)
  105.                     {
  106.                      FailurePoint4:
  107.                         DisposeBitmap(MouseDownUnselected);
  108.                         goto FailurePoint3;
  109.                     }
  110.             }
  111.         BitmapReferenceCount += 1;
  112.         TheButton = (CheckBoxRec*)AllocPtrCanFail(sizeof(CheckBoxRec),"CheckBox");
  113.         if (TheButton == NIL)
  114.             {
  115.              OtherFailurePoint:
  116.                 BitmapReferenceCount -= 1;
  117.                 if (BitmapReferenceCount == 0)
  118.                     {
  119.                         DisposeBitmap(MouseDownSelected);
  120.                         goto FailurePoint4;
  121.                     }
  122.                  else
  123.                     {
  124.                         return NIL;
  125.                     }
  126.             }
  127.         TheButton->Window = Window;
  128.         TheButton->Name = StringToBlockCopy(Name);
  129.         if (TheButton->Name == NIL)
  130.             {
  131.                 ReleasePtr((char*)TheButton);
  132.                 goto OtherFailurePoint;
  133.             }
  134.         TheButton->X = X;
  135.         TheButton->Y = Y;
  136.         TheButton->Width = Width;
  137.         TheButton->Height = Height;
  138.         TheButton->State = False;
  139.         return TheButton;
  140.     }
  141.  
  142.  
  143. /* dispose of a check box and internal data */
  144. void                        DisposeCheckBox(CheckBoxRec* TheButton)
  145.     {
  146.         BitmapReferenceCount -= 1;
  147.         ERROR(BitmapReferenceCount < 0,PRERR(ForceAbort,
  148.             "DisposeCheckBox:  bitmap reference count is negative"));
  149.         if (BitmapReferenceCount == 0)
  150.             {
  151.                 DisposeBitmap(NormalUnselected);
  152.                 DisposeBitmap(NormalSelected);
  153.                 DisposeBitmap(MouseDownUnselected);
  154.                 DisposeBitmap(MouseDownSelected);
  155.             }
  156.         ReleasePtr(TheButton->Name);
  157.         ReleasePtr((char*)TheButton);
  158.     }
  159.  
  160.  
  161. /* find out where the box is located */
  162. OrdType                    GetCheckBoxXLoc(CheckBoxRec* TheButton)
  163.     {
  164.         CheckPtrExistence(TheButton);
  165.         return TheButton->X;
  166.     }
  167.  
  168.  
  169. /* find out where the box is located */
  170. OrdType                    GetCheckBoxYLoc(CheckBoxRec* TheButton)
  171.     {
  172.         CheckPtrExistence(TheButton);
  173.         return TheButton->Y;
  174.     }
  175.  
  176.  
  177. /* find out where the box is located */
  178. OrdType                    GetCheckBoxWidth(CheckBoxRec* TheButton)
  179.     {
  180.         CheckPtrExistence(TheButton);
  181.         return TheButton->Width;
  182.     }
  183.  
  184.  
  185. /* find out where the box is located */
  186. OrdType                    GetCheckBoxHeight(CheckBoxRec* TheButton)
  187.     {
  188.         CheckPtrExistence(TheButton);
  189.         return TheButton->Height;
  190.     }
  191.  
  192.  
  193. /* change the location of the box */
  194. void                        SetCheckBoxLocation(CheckBoxRec* TheButton,
  195.                                     OrdType X, OrdType Y, OrdType Width, OrdType Height)
  196.     {
  197.         CheckPtrExistence(TheButton);
  198.         TheButton->X = X;
  199.         TheButton->Y = Y;
  200.         TheButton->Width = Width;
  201.         TheButton->Height = Height;
  202.     }
  203.  
  204.  
  205. /* internal utility routine that actually does the redrawing */
  206. static void            InternalRedrawCheckBox(CheckBoxRec* TheButton, MyBoolean Hilited)
  207.     {
  208.         OrdType            YStart;
  209.  
  210.         CheckPtrExistence(TheButton);
  211.         SetClipRect(TheButton->Window,TheButton->X,TheButton->Y,
  212.             TheButton->Width,TheButton->Height);
  213.         YStart = (TheButton->Height - 12) / 2 + TheButton->Y;
  214.         if (TheButton->State)
  215.             {
  216.                 if (Hilited)
  217.                     {
  218.                         DrawBitmap(TheButton->Window,TheButton->X,YStart,MouseDownSelected);
  219.                     }
  220.                  else
  221.                     {
  222.                         DrawBitmap(TheButton->Window,TheButton->X,YStart,NormalSelected);
  223.                     }
  224.             }
  225.          else
  226.             {
  227.                 if (Hilited)
  228.                     {
  229.                         DrawBitmap(TheButton->Window,TheButton->X,YStart,MouseDownUnselected);
  230.                     }
  231.                  else
  232.                     {
  233.                         DrawBitmap(TheButton->Window,TheButton->X,YStart,NormalUnselected);
  234.                     }
  235.             }
  236.         DrawTextLine(TheButton->Window,GetScreenFont(),9,TheButton->Name,
  237.             PtrSize(TheButton->Name),TheButton->X + 12 + 6,YStart,ePlain);
  238.     }
  239.  
  240.  
  241. /* redraw the box completely */
  242. void                        RedrawCheckBox(CheckBoxRec* TheButton)
  243.     {
  244.         InternalRedrawCheckBox(TheButton,False);
  245.     }
  246.  
  247.  
  248. /* handle mouse downs.  returns True if the state of the box changed. */
  249. MyBoolean                CheckBoxMouseDown(CheckBoxRec* TheButton, OrdType X, OrdType Y)
  250.     {
  251.         MyBoolean                Inside;
  252.         MyBoolean                OldInside;
  253.  
  254.         OldInside = False;
  255.         do
  256.             {
  257.                 Inside = CheckBoxHitTest(TheButton,X,Y);
  258.                 if (Inside != OldInside)
  259.                     {
  260.                         InternalRedrawCheckBox(TheButton,Inside);
  261.                         OldInside = Inside;
  262.                     }
  263.             } while (GetAnEvent(&X,&Y,NIL,NIL,NIL,NIL) != eMouseUp);
  264.         if (Inside)
  265.             {
  266.                 TheButton->State = !TheButton->State;
  267.             }
  268.         InternalRedrawCheckBox(TheButton,False);
  269.         return Inside;
  270.     }
  271.  
  272.  
  273. /* force the state of the box to a certain value */
  274. void                        SetCheckBoxState(CheckBoxRec* TheButton, MyBoolean TheState)
  275.     {
  276.         CheckPtrExistence(TheButton);
  277.         TheButton->State = TheState;
  278.         RedrawCheckBox(TheButton);
  279.     }
  280.  
  281.  
  282. /* get the state of the box */
  283. MyBoolean                GetCheckBoxState(CheckBoxRec* TheButton)
  284.     {
  285.         CheckPtrExistence(TheButton);
  286.         return TheButton->State;
  287.     }
  288.  
  289.  
  290. /* check to see if the specified location is in the box */
  291. MyBoolean                CheckBoxHitTest(CheckBoxRec* TheButton, OrdType X, OrdType Y)
  292.     {
  293.         return (X >= TheButton->X) && (Y >= TheButton->Y)
  294.             && (X < TheButton->X + TheButton->Width) && (Y < TheButton->Y + TheButton->Height);
  295.     }
  296.